/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.tabs.finalsegment;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import systole.domain.analysis.results.AnalysisResult;
import systole.domain.analysis.results.Parameter;
import systole.view.tabs.controllers.ResultsModel;

/**
 *
 * @author user
 */
public class SignalParamsModel extends AbstractTableModel implements Observer {

    private JTable table;
    private List<Parameter> parameters;
    private String[] columnNames = {"Parámetros", "X", "Y", "X", "Y", "Valor"};
    private static final int PARAM_INDEX = 0;
    private static final int X1_INDEX = 1;
    private static final int Y1_INDEX = 2;
    private static final int X2_INDEX = 3;
    private static final int Y2_INDEX = 4;
    private static final int VALUE_INDEX = 5;
    private static final int PARAMS_AMOUNT = 4;
    public static final int AOD_INDEX = 0;
    public static final int AOS_INDEX = 1;
    public static final int T_INDEX = 2;
    public static final int IAR_INDEX = 3;

    public SignalParamsModel(ResultsModel resultsModel, JTable table) {
        super();
        this.parameters = new ArrayList<Parameter>();
        this.table = table;
        resultsModel.addObserver(this);
        this.loadParams(resultsModel.getCurrentResults());
    }

    private void loadParams(AnalysisResult result) {
        this.parameters.clear();
        if (result != null) {
            // el orden es importante esta definido en las constantes
            this.parameters.add(result.getAod());
            this.parameters.add(result.getAos());
            this.parameters.add(result.getT());
            this.parameters.add(result.getIar());
        }
    }

    public Parameter getParameter(int row) {
        return ((this.parameters.size() > row) ? this.parameters.get(row) : null);
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    public void update(Observable o, Object arg) {
        AnalysisResult analysisResult = (AnalysisResult) arg;
        this.loadParams(analysisResult);
        this.table.revalidate();
        this.fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        return PARAMS_AMOUNT;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return this.columnNames[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case PARAM_INDEX:
                return String.class;
            case X1_INDEX:
            case Y2_INDEX:
            case X2_INDEX:
            case Y1_INDEX:
                return Double.class;
            case VALUE_INDEX:
                return Float.class;
        }
        return null;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case PARAM_INDEX:
                return this.parameters.get(rowIndex).getDescription();
            case X1_INDEX:
                return this.parameters.get(rowIndex).getInitPos().getxCoord();
            case Y1_INDEX:
                return this.parameters.get(rowIndex).getInitPos().getyCoord();
            case X2_INDEX:
                return this.parameters.get(rowIndex).getEndPos().getxCoord();
            case Y2_INDEX:
                return this.parameters.get(rowIndex).getEndPos().getyCoord();
            case VALUE_INDEX:
                return this.parameters.get(rowIndex).calculateParamValue();
        }
        return null;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case PARAM_INDEX:
                this.parameters.get(rowIndex).setDescription((String) aValue);
                break;
            case X1_INDEX:
                this.parameters.get(rowIndex).getInitPos().setxCoord((BigDecimal) aValue);
                break;
            case Y1_INDEX:
                this.parameters.get(rowIndex).getInitPos().setyCoord((BigDecimal) aValue);
                break;
            case X2_INDEX:
                this.parameters.get(rowIndex).getEndPos().setxCoord((BigDecimal) aValue);
                break;
            case Y2_INDEX:
                this.parameters.get(rowIndex).getEndPos().setyCoord((BigDecimal) aValue);
                break;
        }
    }

}
